﻿@model Epi.Web.MVC.Models.FormResponseInfoModel
@{
           WebGrid webGrid = new WebGrid(rowsPerPage: 20,
               canSort: false, canPage: false, ajaxUpdateContainerId: "recordstable"); // Model.SurveyAnswerResponse.pageSize
           webGrid.Bind(Model.ResponsesList, autoSortAndPage: false, rowCount: Model.NumberOfResponses); //Model.SurveyAnswerResponse.pageSize * Model.ResponsesList.Count TBD
}

<script>
    function OpenLinkExistingDialog() {
        ReadExistingLinkResponses($("#LinkFormId_HIDDEN_0").val(), '1', '', '', $("#FROM_RESPONSEID_HIDDEN").val())
        $("#LinkExistingdialog").dialog("open");
    }
    $(document).ready(function () {

        if ('@Model.ParentResponseId'=="")
        {
            //alert();
            $('#FormLink2').hide();
              }
    });
    // function UnLink(){
    $("#UnLinkdialog").dialog({
        autoOpen: false,
        show: "blind",
        hide: "blind",
        resizable: false,
        height: 160,
        width: 420,
        modal: true,
        buttons: [{
            "text": 'Yes',
            "click": function () {

                $.ajax({
                    url: '@Url.Action("UnLink", "Home")' + '?FromResponseId=' + $("#FROM_RESPONSEID_HIDDEN").val() + '&ToResponseId=' + $("#UnLinkdialog").data('ResponseId'),
                    type: 'POST',
                    cache: false,
                    contentType: 'application/json; charset=utf-8',
                    success: successFunc,
                    error: errorFunc
                });
                //                    $.ajaxSetup({ cache: false });
                function successFunc(data, status) {
                    $("#UnLinkdialog").dialog("close");

                    ReadLinkResponses($("#LinkFormId_HIDDEN_0").val(), '@Session["PageNumber"]', '', '', $("#FROM_RESPONSEID_HIDDEN").val());
                }
                function errorFunc() {
                    alert('error');
                }
            },
            "class": 'dialogbtn'
        }, {
            "text": 'No',
            "click": function () {
                $(this).dialog("close");
            },
            "class": 'dialogbtn'
        }]
    });
    //}


</script>

<div id="FormLink2">
    <div id="formtitle" class="formtitle">
        <h2 class="surveyTitle" style="line-height: 120%;">
            @Model.FormInfoModel.FormName <smallcaps>(@Model.ResponsesList.Count)</smallcaps>
        </h2>


    </div>

    <div id="editsettings" class="editsettings">

        <button type="submit" name="AddNewLinkResponse" class="default record" value="@Model.FormInfoModel.FormId/@Model.ParentResponseId">Add New Contact</button>
        <button type="button" id="LinkExistingResponse" name="LinkExistingResponse" class="default" value="@Model.ParentResponseId" onclick="OpenLinkExistingDialog()" style="padding: 4px 12px;">Link Existing Contact</button>

        @*
            <span>&nbsp;&nbsp;</span>
            <button type="submit" name="AddExistingContact" class="default record" value="">Add Existing Contact</button>*@


    </div>
    <div style="clear: both"></div>
    <div id="recordscontent" class="recordscontent">
        <div style="clear: both; margin-bottom: 10px; padding: 0px 0 0;">



            <div id="Linkrecordstable" class="recordstable contact">
                @{
                var gridColumns = new List<WebGridColumn>();


                gridColumns.Add(webGrid.Column(header: "",
                    format: (item) => new HtmlString("<button name='EditForm' type='submit'  class='default edit'  id='" + item.Column0 + "'value='" + item.Column0 + "'>Open</button> "
                        //+"<button name='DeleteButton' type='submit'  class='default edit'  id='" + item.Column0 + "'value='" + item.Column0 + "'> Delete</button>"

                        + "<button name='UnlinkButton' type='submit'  class='default edit'  id='" + item.Column0 + "'value='" + item.Column0 + "'> Unlink</button>")));
                if (Model.Columns.Count >= 1)
                {
                    gridColumns.Add(webGrid.Column("Column1", (Model.Columns.Count >= 1) ? Model.Columns[0].Value : ""));
                }
                if (Model.Columns.Count >= 2)
                {
                    gridColumns.Add(webGrid.Column("Column2", (Model.Columns.Count >= 2) ? Model.Columns[1].Value : ""));
                }
                if (Model.Columns.Count >= 3)
                {
                    gridColumns.Add(webGrid.Column("Column3", (Model.Columns.Count >= 3) ? Model.Columns[2].Value : ""));
                }
                if (Model.Columns.Count >= 4)
                {
                    gridColumns.Add(webGrid.Column("Column4", (Model.Columns.Count >= 4) ? Model.Columns[3].Value : ""));
                }
                if (Model.Columns.Count >= 5)
                {
                    gridColumns.Add(webGrid.Column("Column5", (Model.Columns.Count >= 5) ? Model.Columns[4].Value : ""));
                }
                @webGrid.GetHtml( columns: gridColumns);




                }
                @Html.Hidden("hidFormId", @Model.FormInfoModel.FormId)
                @Html.Hidden("hidPageNumber", @Model.CurrentPage)
                @{
                if (Model.NumberOfPages == 0)
                {
                    <div class="zerorec">
                        No records found.
                    </div>
                }
                }
            </div>







        </div>
    </div>

    <div id="UnLinkdialog" title="Unlink Record">
        <p style="font-size:1.20em; font-weight:600;">Are you sure you want to unlink this record? </p>
    </div>
</div>